package and.xunger.com.myfirstdemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;

import and.xunger.com.myfirstdemo.tablayout.TabFragment;

public class CoordinatorLayoutActivity extends AppCompatActivity {

  private AppBarLayout appBarLayout;
  private Toolbar toolbar;
  private TabLayout tabLayout;
  private ViewPager viewPager;
  private Bitmap bitmap;
  private LinearLayout llHeaderContainer;
  private CollapsingToolbarLayout collapsingToolbarLayout;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coordinator_layout);

    initView();
    initToolBar();
    initViewPager();
  }

  private void initView() {
    appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    viewPager = (ViewPager) findViewById(R.id.view_pager);
    llHeaderContainer = (LinearLayout) findViewById(R.id.header);
    collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_tool_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  }

  private void initToolBar() {
    bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override

      public void onClick(View v) {
        onBackPressed();
      }
    });
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
      @Override
      public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset <= -llHeaderContainer.getHeight() + 200) {
          collapsingToolbarLayout.setTitle("Coordinator");
        } else {
          collapsingToolbarLayout.setTitle(" ");
        }
      }
    });

  }

  private void initViewPager() {
    final ArrayList<TabFragment> fragments = new ArrayList<>();
    fragments.add(new TabFragment());
    fragments.add(new TabFragment());
    fragments.add(new TabFragment());
    viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
      @Override
      public Fragment getItem(int position) {
        return fragments.get(position);
      }

      @Override
      public int getCount() {
        return fragments.size();
      }

    });
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
  }
}
